<?php

namespace iBrand\Shop\Core\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class Balance extends Model
{
    protected $table = 'el_balance';
    protected $guarded = ['id'];

    const TYPE_RECHARGE = 'recharge';
    const TYPE_ORDER_PAYMENT = 'order_payment';
    const TYPE_REFUND = 'refund';

    public function origin()
    {
        return $this->morphTo();
    }

    public function scopeSumBalance($query)
    {
        return $query->sum('value');
    }

    public function scopeSumByUser($query, $userId)
    {
        return $query->where('user_id', $userId)->sum('value');
    }

    public function payment()
    {
        return $this->hasOne(Payment::class, 'id', 'origin_id');
    }

}
